//
//  JKTagLabel.m
//  JackToolsBox
//
//  Created by Jack Xue on 2019/12/10.
//  Copyright © 2019 Jack Xue. All rights reserved.
//

#import "JKTagLabView.h"

@interface JKTagLabView(){
    NSMutableArray *_rectArray; //用于确定点击事件
    CGPoint _touchDownPoint;
}
@property(nonatomic,assign)NSInteger count;
@end

@implementation JKTagLabView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        _rectArray = [NSMutableArray arrayWithCapacity:0];
        
        _touchDownPoint = CGPointZero;
    }
    return self;
}

#pragma mark - private function
- (void)prepareData
{
    //1.获取需要展示的tag个数
    if (self.dataSource && [self.dataSource respondsToSelector:@selector(numberOfItemsForTagCell)]) {
        _count = [self.dataSource numberOfItemsForTagCell];
    }else{
        return;
    }
    
    //2.循环，添加每个cell
    if (self.dataSource && [self.dataSource respondsToSelector:@selector(tagCellAtIndex:)]) {
        for (NSInteger i = 0; i < _count; i++) {
            JKTagPart *cell = [self.dataSource tagCellAtIndex:i];
            [self addSubview:cell];
        }
    }//@end - 结束添加subView
}

- (void)resetView
{
    for (UIView *subView in self.subviews) {
        if (subView && [subView isKindOfClass:[JKTagPart class]]) {
            [subView removeFromSuperview];
        }
    }
}

#pragma mark - 实现展示
-(void)layoutSubviews{
    [super layoutSubviews];
    
    //先释放原来
    [self resetView];
    [self prepareData];
    
    //3.分别获取每个subview的frame，安排位置
    CGFloat widthRec = 0, heightRec = 0, lastHeight = 0;
    for (UIView *view in self.subviews) {
        if ([view isKindOfClass:[JKTagPart class]]) {
            JKTagPart *subLabel = (JKTagPart *)view;
            CGRect newRect = subLabel.frame;
            if (widthRec + subLabel.frame.size.width + Jk_ITEM_SPACE > self.frame.size.width) {
                //需要换行，heightRec当前subView顶点，lastHeight当前subview底部，用于确定最后self.height
                heightRec = heightRec + subLabel.frame.size.height + Jk_ITEM_SPACE;
                lastHeight = heightRec + subLabel.frame.size.height;
            }else{
                //左顶点上次已经计算好了，直接使用即可
                newRect.origin.x = widthRec;
            }
            newRect.origin.y = heightRec;
            subLabel.frame = newRect;
            widthRec = newRect.origin.x + newRect.size.width + Jk_ITEM_SPACE;
            if ([_delegate respondsToSelector:@selector(didSelectedLabelTagWithIndex:)]) {
                NSValue *recValue = [NSValue valueWithCGRect:newRect];
                [_rectArray addObject:recValue];
            }
        }
    }
    CGRect viewRect = self.frame;
    viewRect.size.height = lastHeight;
    self.frame = viewRect;
}

-(void)drawRect:(CGRect)rect{
    [super drawRect:rect];

}

#pragma mark - 点击事件
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesBegan:touches withEvent:event];
    
    _touchDownPoint = [[touches anyObject] locationInView:self];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [super touchesEnded:touches withEvent:event];
    
    CGPoint point = [[touches anyObject] locationInView:self];
    
    if (!_rectArray && _rectArray.count <= 0) {
        return;
    }
    
    for (NSInteger i = 0; i < _rectArray.count; i++) {
        NSValue *value = _rectArray[i];
        CGRect rect = [value CGRectValue];
        //直接判断began & end的点在一个rect内
        if (CGRectContainsPoint(rect, point) && CGRectContainsPoint(rect, _touchDownPoint)) {
            if (_delegate && [_delegate respondsToSelector:@selector(didSelectedLabelTagWithIndex:)]) {
                [_delegate didSelectedLabelTagWithIndex:i];
                return;
            }
        }
    }
}

@end
